//无限获得方块
function CreateBlock(){
    var Lines = document.createElement('div');
    Lines.className='Line-Block';
    
    Lines.style.transition='Move 1s linear 1s'
    var i = Math.floor(Math.random()*4);
    for (let index = 0; index < 4; index++) {
        
        if(index==i){
            var Black_Block = document.createElement('span');
            Black_Block.className='Black-Block';
            Lines.appendChild(Black_Block);
        }
        else{
            var White_Block = document.createElement('span');
            White_Block.className='White-Block';
            Lines.appendChild(White_Block);
        }
    }


    //将获得的方块添加到行
    var Game_Line = document.getElementById('Game-Line');
    Game_Line.insertBefore(Lines,Game_Line.childNodes[0]);
    BlackBottom();
    //让行向下移动
    var Container = document.getElementById('Game-Line');
    Container.style.transform='Move 0.5s linear infinite';
    //刷新点击事件让新产生的格子获得属性
    ClickBlock();
}

//计算黑色格子与底部的距离
function BlackBottom() {
    var Black_Block = document.getElementsByClassName('Black-Block');
    for (let index = 0; index < Black_Block.length; index++) {
        let element = Black_Block[index];
        if(element.offsetTop>710){
            //如果黑色格子触底则游戏结束
            let Lose = document.getElementById('Loser');
            Lose.style.height="300px"
            Lose.style.transition="all 0.5s"
            Lose.style.zIndex='6';
        }
    }

    //白色格子掉落底部后，删除该行节点

    // var Points =document.getElementById('Game-Line')
    // var Grey_Block = document.getElementsByClassName('White-Block');
    // for (let i = 0; i < Grey_Block.length; i++) {
    //     const elements = Grey_Block[i];
    //     if(elements.offsetTop>710){
    //         Points.removeChild(Points.childNodes[Grey_Block.length]);
    //     }
    // }
    
} 
GameStart()
function GameStart() {
    var Start = document.getElementById('Game-Start')
    Start.addEventListener('click',function Begin(){
    Start.className='Grey-Block'; 
    CreateBlock()
    setInterval(function () {
         timer.innerHTML=showtime();
         CreateBlock();
        },1000);
    })
}
var score = 0;
//格子点击事件
function ClickBlock() {
    
    var Block = document.getElementsByTagName('span');
    for (let index = 0; index < Block.length; index++) {
        const element = Block[index];
        if(element.className=='Black-Block'){
        element.addEventListener('click',function(){
            score=score+1;
            console.log(score)
            element.className='Grey-Block';
            var Black_Block = document.getElementsByClassName('Grey-Block');
            console.log(Black_Block.length);
            var scores = document.getElementById('scores');
            scores.innerHTML=Black_Block.length;
            var sc = document.getElementById('score');
            sc.innerHTML=Black_Block.length;
            let Tscore = document.getElementById('TOscore');
            Tscore.innerHTML=Black_Block.length
            
        })
        }else if(element.className=='White-Block'){
            element.addEventListener('click',function () {
                element.className='Red-Block';
                let Lose = document.getElementById('Loser');
                Lose.style.height="300px"
                Lose.style.transition="all 0.5s"
                Lose.style.zIndex='6';
                var BC = document.getElementById('BC');
                BC.style.opacity='1';
                BC.style.zIndex='3';
            })
        };
    }
    }



//计时函数
var timer = document.getElementById('timer');
timer.innerHTML='20秒'
let seconds = '20';
function showtime() {
    if(seconds>0){
    seconds=seconds-1;

    return seconds + "秒";
}
    else{
        let Fi = document.getElementById('Finish');
                Fi.style.height="300px"
                Fi.style.transition="all 0.5s"
                Fi.style.zIndex='5';
                var BC = document.getElementById('BC');
                BC.style.opacity='1';
                BC.style.zIndex='3';

        return 0+"秒";
    }
}